package com.kokomi.service.impl;

import com.kokomi.mapper.BookMapper;
import com.kokomi.pojo.Book;
import com.kokomi.pojo.RedisConst;
import com.kokomi.pojo.VO.CartItemVo;
import com.kokomi.pojo.VO.CartVo;
import com.kokomi.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate<String, CartVo> redisTemplate;

    @Autowired
    private BookMapper bookMapper;

    /**
     * 获取用户的购物车
     * @param userId
     */
    @Override
    public CartVo getCart(Long userId) {
        // 1.从redis中根据用户id获取购物车
        ValueOperations<String, CartVo> ValueOperations = redisTemplate.opsForValue();
        CartVo cartVo = ValueOperations.get(RedisConst.USER_CART_PREFIX + ":" + userId);
        // 2.判断购物车对象是否为null
        if (cartVo == null) {
            // 3.如果为null, 创建一个购物车对象，保存到redis中
            cartVo = new CartVo();
            cartVo.setUserId(userId);
            ValueOperations.set(RedisConst.USER_CART_PREFIX + ":" + userId, cartVo);
        }

        // 4.如果不为null，直接返回
        return cartVo;
    }



    /**
     * 更新购物车中商品的数量
     * @param userId
     * @param bookId
     * @param quantity
     */
    @Override
    public CartVo updateCartItemQuantity(Long userId, Long bookId, Integer quantity) {
        CartVo cartVo = getCart(userId);
        cartVo.updateItemQuantity(bookId, quantity);
        redisTemplate.opsForValue().set(RedisConst.USER_CART_PREFIX + ":" + userId,
                cartVo,
                RedisConst.USER_CART_EXPIRE,
                TimeUnit.DAYS);
        return cartVo;
    }

    /**
     * 添加一件商品到购物车
     * @param userId
     * @param bookId
     */
    @Override
    public CartVo addCartItem(Long userId, Long bookId) {
        //获取某个用户的购物车对象
        CartVo cartVo = getCart(userId);

        //通过bookId从数据库中获取书籍对象
        Book book = bookMapper.selectById(bookId);

        //调用方法添加到购物车中，数量是1本
        CartItemVo cartItem = new CartItemVo(userId
                ,bookId, book.getName()
                ,book.getImgSrc()
                ,book.getPrice(), 1);
        cartVo.addItem(cartItem);

        //将购物车对象保存到Redis中7天
        redisTemplate.opsForValue().set(RedisConst.USER_CART_PREFIX + ":" + userId,
                cartVo,
                RedisConst.USER_CART_EXPIRE,
                TimeUnit.DAYS);

        return cartVo;
    }

    /**
     * 清空购物车
     *
     * @param userId
     */
    @Override
    public void clearCart(Long userId) {
        CartVo cartVo = getCart(userId);
        cartVo.clearCart();
        redisTemplate.delete(RedisConst.USER_CART_PREFIX + ":" + userId);
    }

    /**
     * 删除购物车项
     * @param userId
     * @param bookId
     */
    @Override
    public CartVo removeCartItem(Long userId, Long bookId) {
        CartVo cartVo = getCart(userId);
        cartVo.removeItem(bookId);
        redisTemplate.opsForValue().set(RedisConst.USER_CART_PREFIX + ":" + userId,
                cartVo,
                RedisConst.USER_CART_EXPIRE,
                TimeUnit.DAYS);

        return cartVo;
    }
}
